<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" size="small">
      <el-form-item label="流程名称" prop="instanceName">
        <el-input v-model="queryParams.instanceName" placeholder="请输入流程名称" clearable style="width: 240px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tastList">
      <!--      <el-table-column label="流程ID" align="center" prop="id"/>-->
      <el-table-column label="流程名称" align="center" prop="instanceName" />
      <el-table-column label="任务节点名称" align="center" prop="name" />
      <!--      <el-table-column label="任务状态" align="center" prop="status"/>
      <el-table-column label="附件" align="center" prop="attachmentLink">
        <template slot-scope="scope"  v-if="scope.row.attachmentLink!=null">
          <a @click="preView(scope.row.attachmentLink)" target="_blank" class="buttonText" style="color: #00afff;text-decoration:underline">附件</a>
        </template>
      </el-table-column>-->
      <el-table-column label="办理人" align="center" prop="assignee" />
      <el-table-column label="创建时间" align="center" prop="startTime" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="examineAndApprove(scope.row)"
            v-hasPermi="['workflow:leave:edit']">审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" v-if="open" width="500px" append-to-body>
      <leaveHistoryForm :businessKey="businessKey" v-if="'leave' == definitionKey" />

      <el-form :model="form" ref="form" label-width="100px" class="demo-dynamic">
        <el-form-item v-for="(domain, index) in form.formData" :label="domain.controlLable" :key="index">
          <el-radio-group v-model="domain.controlValue" v-if="'radio' == domain.controlType">
            <el-radio v-for="(defaults, indexd) in domain.controlDefault.split('--__--')" :label=indexd :key="indexd">{{
              defaults }}

            </el-radio>
            <!--<el-radio value="1" label="1" >同意</el-radio>
            <el-radio value="0" label="0" >不同意</el-radio>-->
          </el-radio-group>
          <el-input type="textarea" v-model="domain.controlValue" v-if="'textarea' == domain.controlType"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 其他审批单 -->
    <el-dialog title="审批" :visible.sync="otherOpen" v-if="otherOpen" width="500px" append-to-body
      @closeDelDialog="closeDelDialog">
      <el-form ref="otherForm" :model="otherForm" :rules="otherRules" label-width="120px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="审批意见：">
              <el-radio-group v-model="otherForm.resource">
                <el-radio :label="1">同意</el-radio>
                <el-radio :label="0">不同意</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="批 注：">
              <el-input v-model="otherForm.note" type="textarea" :rows="3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="closeDelDialog">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 借阅审批单 -->
    <archiveBorrow title="借阅审批" :open="archiveBorrowOpen" istype="2" @closeDelDialog="closeDelDialog"
      ref="archiveBorrowRef"></archiveBorrow>
    <!-- 销毁审批单 -->
    <archiveDestroy title="销毁审批" :open="archiveDestroyOpen" istype="2" @closeDelDialog="closeDelDialog"
      ref="archiveDestroyRef"></archiveDestroy>
    <!-- 归档审批单 -->
    <archiveSave title="归档审批" :open="archiveSaveOpen" istype="2" @closeDelDialog="closeDelDialog" ref="archiveSaveRef">
    </archiveSave>

    <!-- 预归档审批单 -->
    <yarchiveSave title="预归档审批" :open="yarchiveSaveOpen" istype="2" @closeDelDialog="closeDelDialog"
      ref="yarchiveSaveRef"></yarchiveSave>



    <!-- 密级审批单 -->
    <!-- isType:1 代表从待办任务打开的 -->
    <archiveMjIdentify title="密级审批" :open="archiveMjIdentifyOpen" @closeDelDialog="closeDelDialog"
      ref="archiveMjIdentifyRef" isType="1"></archiveMjIdentify>

    <!-- 档案移交审批单 -->
    <!-- isType:1 代表从待办任务打开的 -->
    <archiveHandOver v-if="archiveHandOverOpen" :open="archiveHandOverOpen" @closeDelDialog="closeDelDialog"
      ref="archiveHandOverRef" :printRow="archiveHandOverRow" :isType="2" :printTitle='printTitle' :printShow="false"
      :source="source"></archiveHandOver>

    <!-- 保管期限审批单 -->
    <!-- isType:1 代表从待办任务打开的 -->
    <archiveBgqxIdentify title="保管期限审批" :open="archiveBgqxIdentifyOpen" @closeDelDialog="closeDelDialog"
      ref="archiveBgqxIdentifyRef" isType="1"></archiveBgqxIdentify>

    <!-- 編輯发布 istype="1"   待办 istype="2"  已办 istype="3" -->
    <approve title="编辑发布审批" :open="approveOpen" istype="2" @closeDelDialog="closeDelDialog" ref="approveRef"></approve>
  </div>
</template>

<script>


import { formDataSave, getTaskById, listTask } from "@/api/activiti/task";
import leaveHistoryForm from "@/views/workflow/leave/leaveHistoryForm";
import { getDicts_archive } from "@/api/archive/dict/dictManageData";

import archiveBorrow from "@/views/archive/archiveBorrow/archiveBorrowReceipt/notQrocessed/approve"; // 借阅审批单
import archiveDestroy from "@/views/archive/archiveDestroy/archiveDestroyReceipt/notQrocessed/approve"; // 销毁审批单
import archiveSave from "@/views/archive/fileCollection/archiveApproval/notQrocessed/approve"; // 归档审批单
import yarchiveSave from "@/views/archive/fileCollection/preArchiving/notQrocessed/approve"; // 预归档审批单
import archiveMjIdentify from "@/views/archive/archivalAppraisal/classification/examineApprove/detail"; // 密级审批单
import archiveHandOver from "@/views/archive/archiveTransfer/offline/printOrder.vue"; //移交四性检测详情
import archiveBgqxIdentify from "@/views/archive/archivalAppraisal/bgqxIdentify/bgqxExamineApprove/detail"; //保管期限审批单
import approve from "@/views/archive/filesManage/editReleaseManage/approvalPending/approve.vue"

export default {
  name: "Leave",
  components: { leaveHistoryForm, archiveBorrow, archiveSave, archiveMjIdentify, archiveHandOver, yarchiveSave, archiveBgqxIdentify, approve, archiveDestroy },
  data() {
    return {
      id: '',
      definitionKey: '',
      businessKey: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假表格数据
      tastList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        instanceName: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        formData: []
      },
      // 表单校验
      rules: {},
      // 流程字典
      flowPathOption: [],
      // 借阅审批
      archiveBorrowOpen: false,
      // 销毁审批
      archiveDestroyOpen: false,
      // 归档审批
      archiveSaveOpen: false,
      // 密级审批
      archiveMjIdentifyOpen: false,
      // 预归档审批
      yarchiveSaveOpen: false,
      // 移交单审批
      archiveHandOverOpen: false,
      // 保管期限审批
      archiveBgqxIdentifyOpen: false,
      //编辑发布审批
      approveOpen: false,
      // 其他审批
      otherOpen: false,
      otherForm: {
        resource: undefined,
        note: undefined
      },
      archiveHandOverRow: "",
      otherRules: {
        resource: [
          { required: true, message: "审批意见不能为空", trigger: "blur" }
        ]
      },
      printTitle: "档案移交",
      source: ''
    };
  },
  mounted() {
  },
  created() {
    debugger
    this.getList();
    this.getOption()
    if (this.$route.query.taskId != undefined) {
      getTaskById(this.$route.query.taskId).then(res => {
        if (res != undefined && res != null && res != '') {
          this.examineAndApprove(res)
        }else {
          this.$modal.msgWarning("数据已办，没有待审批的数据！");
        }
      })
    }
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 查询请假列表 */
    getList() {
      this.loading = true;
      listTask(this.queryParams).then(response => {
        this.tastList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取流程定义字典
    getOption() {
      getDicts_archive('WORK_FLOW_KEY').then(response => {
        this.flowPathOption = response.data.map(item => {
          return {
            value: item.dictValue,
            label: item.dictLabel
          }
        })
      })
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.definitionKey = '',
        this.businessKey = '',
        this.form = {
          formData: [],
        };
      this.resetForm("form");
    },

    /** 审批按钮操作 */
    examineAndApprove(row) {
      console.log(row, "-----------------------")
      this.reset();
      this.definitionKey = row.definitionKey;
      this.businessKey = row.businessKey;
      this.id = row.id;
      const obj = this.flowPathOption.find(item => item.label == row.definitionKey)
      console.log(obj)
      if (obj == undefined) {
        this.otherOpen = true
      } else {
        const str = obj.value

        // 借阅
        if (str == 'archiveBorrow') {
          this.$nextTick(() => {
            this.$refs.archiveBorrowRef.init(row);
          })
          this.archiveBorrowOpen = true
        } else if (str == 'archiveDestroy') {//销毁
          this.$nextTick(() => {
            this.$refs.archiveDestroyRef.init(row);
          })
          this.archiveDestroyOpen = true
        } else if (str == 'archiveSave') { // 归档
          this.$nextTick(() => {
            this.$refs.archiveSaveRef.init(row);
          })
          this.archiveSaveOpen = true
        } else if (str == 'archiveMjIdentify') { // 密级
          this.$nextTick(() => {
            this.$refs.archiveMjIdentifyRef.activeName = '0'
            this.$refs.archiveMjIdentifyRef.init(row);
          })
          this.archiveMjIdentifyOpen = true
        } else if (str == 'archiveHandOver') { // 档案移交审批单
          this.$nextTick(() => {

            this.archiveHandOverRow = row.processVariables
            // this.$refs.archiveHandOverRef.init(row);
            this.archiveHandOverOpen = true
          })
        } else if (str == 'yarchiveSave') { // 预归档
          this.$nextTick(() => {
            this.$refs.yarchiveSaveRef.init(row);
          })
          this.yarchiveSaveOpen = true
        } else if (str == 'archiveBgqxIdentify') { // 保管期限
          this.$nextTick(() => {
            this.$refs.archiveBgqxIdentifyRef.activeName = '0'
            this.$refs.archiveBgqxIdentifyRef.init(row);
          })
          this.archiveBgqxIdentifyOpen = true
        } else if (str == 'archiveOnlineReceive') { // 在线接收审批单
          this.$nextTick(() => {
            this.archiveHandOverRow = row.processVariables
            this.source = 'receivingApprovalRecords'
            // this.$refs.archiveHandOverRef.init(row);
            this.archiveHandOverOpen = true
          })
        } else if (str == 'archiveBjfb') { // 编辑发布收审批单
          this.$nextTick(() => {
            this.$refs.approveRef.init(row);
          })
          this.approveOpen = true

        }
      }
      this.getList();
      // formDataShow(row.id).then(response => {
      //
      //   // FormProperty_3qipis2--__!!radio--__!!审批意见--__!!i--__!!同意--__--不同意
      //   // FormProperty_0lffpcm--__!!textarea--__!!批注--__!!f--__!!null
      //   let datas = response.data;
      //   let formData = []
      //   for (let i = 0; i < datas.length; i++) {
      //     let strings = datas[i].split('--__!!')
      //     let controlValue = null
      //     let controlDefault = null
      //     switch (strings[1]) {
      //       case 'radio':
      //         controlValue = 1;
      //         controlDefault = strings[4]
      //         break;
      //       // default:
      //     }
      //     formData.push({
      //       controlId: strings[0],
      //       controlType: strings[1],
      //       controlLable: strings[2],
      //       controlIsParam: strings[3],
      //       controlValue: controlValue,
      //       controlDefault: controlDefault
      //     })
      //   }
      //   this.form.formData = formData;
      //   this.open = true;
      //   this.title = "审批";
      // });
    },
    closeDelDialog() {
      this.archiveBorrowOpen = false
      this.archiveSaveOpen = false
      this.yarchiveSaveOpen = false
      this.archiveMjIdentifyOpen = false
      this.archiveHandOverOpen = false
      this.archiveBgqxIdentifyOpen = false
      this.archiveDestroyOpen=false;
      this.approveOpen = false
      this.otherOpen = false
      this.queryParams.pageNum = 1
      delete this.$route.query.taskId;
      this.getList();
    },
    submitChild(childForm) {
      this.otherForm = childForm;
      this.submit();
    },
    // 提交
    submit() {

      var data = []
      if (this.otherForm.resource == '1') {
        data = [{
          controlId: 'is_agree',
          controlDefault: '不同意--__--同意',
          controlIsParam: 'i',
          controlLable: '审批意见',
          controlType: 'radio',
          controlValue: 1
        }, {
          controlId: "comment",
          controlDefault: 'FormProperty_2ugo1se',
          controlIsParam: 'f--!!null',
          controlLable: '批注',
          controlType: 'textarea',
          controlValue: this.otherForm.note
        }];
        data.id = this.id
        formDataSave(this.id, data).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.otherOpen = false
        })
      } else {
        data = [{
          controlId: 'is_agree',
          controlDefault: '不同意--__--同意',
          controlIsParam: 'i',
          controlLable: '审批意见',
          controlType: 'radio',
          controlValue: 0
        }, {
          controlId: "comment",
          controlDefault: 'FormProperty_2ugo1se',
          controlIsParam: 'f--!!null',
          controlLable: '批注',
          controlType: 'textarea',
          controlValue: this.otherForm.note
        }];
        data.id = this.id
        formDataSave(this.id, data).then(response => {
          this.$modal.msgSuccess(response.msg)
          this.otherOpen = false
        })
      }
      this.getList();
    },

    /** 提交按钮 */
    submitForm() {
      formDataSave(this.id, this.form.formData).then(response => {
        this.$modal.msgSuccess("审批成功");
        this.open = false;
        this.getList();
      });
      this.getList();
    },
    /**文档预览**/
    preView(filePath) {
      console.info(filePath);
      if (null == filePath || '' == filePath) {
        this.$alert('申请人没有上传哦', '消息提示', {
          confirmButtonText: '确定',
        });
      } else {
        // let originUrl = 'http://127.0.0.1:80/ruoyi-admin/';   //要预览文件的访问地址'
        window.open(filePath);
      }
      // const bb = originUrl  + aa;
      // window.open('http://127.0.0.1:8012/onlinePreview?url='+encodeURIComponent(Base64.encode(bb)));
    },
  }
};
</script>
